package com.cloud.lawcase.api.provider;

import org.apache.ibatis.jdbc.SQL;

import com.cloud.lawcase.common.openapi.OpenApiCauseStackSearchCriteria;

public class ExotericCauseStackProvider {
	private static final String SELECT = "if(t1.situation_desc is null, '法定情形', t1.situation_desc) situation_desc, t1.punish_id, t2.excute_type_name",
			FROM = "tc_law_rel_cause_excute_type t1, tc_dic_law_excute_type t2, tc_dic_law_cause t3, tc_dic_law t4";

	public String listCauseStack(OpenApiCauseStackSearchCriteria criteria) {
		SQL sql = new SQL().SELECT(SELECT).FROM(FROM);
		sql.WHERE("t1.excute_type_id = t2.excute_type_id and t1.cause_id = t3.cause_id and t3.law_id = t4.law_id");
		sql.WHERE("t3.cause_code = #{causeCode}");
		if (null != criteria.getRegions() && criteria.getRegions().length > 0) {
			StringBuilder region = new StringBuilder();
			boolean b = false;
			for (String r : criteria.getRegions()) {
				if (b) {
					region.append(", ");
				}
				region.append("'").append(r).append("'");
				b = true;
			}
			sql.WHERE("t4.law_region_id in (" + region.toString() + ")");
		}
		return sql.toString();

	}
}
